package com.spzx.common.rabbit.service;

import com.alibaba.fastjson2.JSON;
import com.spzx.common.core.utils.uuid.UUID;
import com.spzx.common.rabbit.entity.GuiguCorrelationData;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RabbitService {
    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    RedisTemplate redisTemplate;

    public boolean sendMessage(String exchange,String routingKey,Object message){
        GuiguCorrelationData correlationData = new GuiguCorrelationData();
        String uuid= "mq:"+ UUID.randomUUID().toString().replaceAll("-","");
        correlationData.setId(uuid);
        correlationData.setMessage(message);
        correlationData.setExchange(exchange);
        correlationData.setRoutingKey(routingKey);

        rabbitTemplate.convertAndSend(exchange, routingKey, message,correlationData);
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(correlationData),10, TimeUnit.MINUTES);

        return true;
    }
    public void sendDeadMessage(String exchange_dead, String routing_dead_1, String hello_dead_letter){

        GuiguCorrelationData correlationData = new GuiguCorrelationData();
        String CORRELATIONDATA_ID = UUID.randomUUID().toString().replace("-","");
        correlationData.setId(CORRELATIONDATA_ID);
        correlationData.setMessage(hello_dead_letter);
        correlationData.setExchange(exchange_dead);
        correlationData.setRoutingKey(routing_dead_1);
        correlationData.setRetryCount(0);// 刚发消息没有重试
        correlationData.setDelay(false);
        correlationData.setDelayTime(0);
        System.out.println("发送消息：" + hello_dead_letter);
        System.out.println(correlationData);
        System.out.println("消息的原始id："+CORRELATIONDATA_ID);

        // 为了保证投递失败后消息的可靠性，将消息存入redis
        redisTemplate.opsForValue().set(CORRELATIONDATA_ID, JSON.toJSONString(correlationData), 10, TimeUnit.SECONDS);
        rabbitTemplate.convertAndSend(exchange_dead, routing_dead_1, hello_dead_letter, new MessagePostProcessor(){

            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");// 过期时间ttl
                return message;
            }
        }, correlationData);
        }


    public void sendDelayMessage(String exchange_delay, String routing_delay, String hello_delay_letter, int delayTime) {
        GuiguCorrelationData correlationData = new GuiguCorrelationData();
        String CORRELATIONDATA_ID = UUID.randomUUID().toString().replace("-","");
        correlationData.setId(CORRELATIONDATA_ID);
        correlationData.setMessage(hello_delay_letter);
        correlationData.setExchange(exchange_delay);
        correlationData.setRoutingKey(routing_delay);
        correlationData.setRetryCount(0);// 刚发消息没有重试
        correlationData.setDelay(true);// 因为延迟队列是立马投递失败的消息会触发回调机制，所以要加入参数用来做判断
        correlationData.setDelayTime(0);
        System.out.println("发送消息：" + hello_delay_letter);
        System.out.println(correlationData);
        System.out.println("消息的原始id："+CORRELATIONDATA_ID);

        // 为了保证投递失败后消息的可靠性，将消息存入redis
        redisTemplate.opsForValue().set(CORRELATIONDATA_ID, JSON.toJSONString(correlationData), 10, TimeUnit.MINUTES);


        rabbitTemplate.convertAndSend(exchange_delay, routing_delay, hello_delay_letter, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(10000);// 过期时间ttl
                return message;
            }
        }, correlationData);
    }
}
